package com.jianyun.jyzs.dao;

import android.content.Context;
import android.text.TextUtils;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;
import com.jianyun.jyzs.bean.WorkOrderDetialResponse;
import com.jianyun.jyzs.db.DatabaseHelper;


import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.rongcloud.rce.lib.CacheTask;
import com.jianyun.jyzs.utils.RceLog;

@SuppressWarnings("unchecked")
public class WorkOrderDao {
    private Dao<WorkOrderDetialResponse.WorkOrder, Integer> workOrderDao;
    private DatabaseHelper dbHelper;

    /**
     * 构造方法
     * 获得数据库帮助类实例，通过传入Class对象得到相应的Dao
     */
    public WorkOrderDao(Context context) {
        try {
            dbHelper = DatabaseHelper.getHelper(context);
            workOrderDao = dbHelper.getDao(WorkOrderDetialResponse.WorkOrder.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 添加一条记录
     */
    public void insert(WorkOrderDetialResponse.WorkOrder order) {
        try {
            workOrderDao.createOrUpdate(order);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public List<WorkOrderDetialResponse.WorkOrder> getListbyCalendar(String userid, String enterpriseCode, String month) {
        Where<WorkOrderDetialResponse.WorkOrder, Integer> where = workOrderDao.queryBuilder().where();
        try {
            where.eq("createUserId", userid).and().eq("enterpriseCode", enterpriseCode).and().like("createDate", month);
            return where.query();
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public List<WorkOrderDetialResponse.WorkOrder> getWorkOrder(String userid, String enterpriseCode, String month) {
        Where<WorkOrderDetialResponse.WorkOrder, Integer> where = workOrderDao.queryBuilder().where();
        try {
            where.eq("createUserId", userid).and().eq("enterpriseCode", enterpriseCode).and().eq("createDate", month);
            return where.query();
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }
    public List<WorkOrderDetialResponse.WorkOrder> getAll(){
        List<WorkOrderDetialResponse.WorkOrder> list = new ArrayList<>();
        QueryBuilder<WorkOrderDetialResponse.WorkOrder, Integer> builder = workOrderDao.queryBuilder();
        try {
            list = builder.query();
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public List<WorkOrderDetialResponse.WorkOrder> getResponseWorkOrder(String userid, String enterpriseCode, String month) {
        Where<WorkOrderDetialResponse.WorkOrder, Integer> where = workOrderDao.queryBuilder().where();
        try {
            where.eq("responUserId", userid).and().eq("enterpriseCode", enterpriseCode).and().eq("createDate", month);
            return where.query();
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 查询所有
     */
    public List<WorkOrderDetialResponse.WorkOrder> getList(String userid, String enterpriseCode) {
        List<WorkOrderDetialResponse.WorkOrder> list = new ArrayList<>();
        try {
            QueryBuilder<WorkOrderDetialResponse.WorkOrder, Integer> builder = workOrderDao.queryBuilder();
            builder.where().eq("createUserId",userid).and().eq("enterpriseCode",enterpriseCode);
            list = builder.query();
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return list;
        }

    }

    /**
     * 查询参与的 我是相关人
     */
    public List<WorkOrderDetialResponse.WorkOrder> getPartakeList(String enterpriseCode,String userid) {
        try {
            QueryBuilder<WorkOrderDetialResponse.WorkOrder, Integer> builder = workOrderDao.queryBuilder().orderBy("id", false);
            Where<WorkOrderDetialResponse.WorkOrder, Integer> where = builder.where();
            where.eq("enterpriseCode", enterpriseCode).and().eq("wotype", 3).and().ne("workState", 1).and().eq("localUserId",userid);
            return where.query();
//            return builder.query();
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 查询所有
     */
    public List<WorkOrderDetialResponse.WorkOrder> getResponseList(String userid, String enterpriseCode) {
        try {
            QueryBuilder<WorkOrderDetialResponse.WorkOrder, Integer> builder = workOrderDao.queryBuilder().orderBy("id", false);
            Where<WorkOrderDetialResponse.WorkOrder, Integer> where = builder.where();
            where.eq("responUserId", userid).and().eq("enterpriseCode", enterpriseCode).and().ne("workState", 1);
            return where.query();
//            return builder.query();
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }

    }


    public WorkOrderDetialResponse.WorkOrder getOrder(String oid, String enterpriseCode) {
        try {
            QueryBuilder<WorkOrderDetialResponse.WorkOrder, Integer> builder = workOrderDao.queryBuilder();
            Where<WorkOrderDetialResponse.WorkOrder, Integer> where = builder.where();
            List<WorkOrderDetialResponse.WorkOrder> query = where.eq("id", oid).and().eq("enterpriseCode",enterpriseCode).query();
            if (query != null) {
                return query.get(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return null;
    }

    public void delOrder(String orderId) {
        try {
            workOrderDao.deleteById(Integer.parseInt(orderId));
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public List<WorkOrderDetialResponse.WorkOrder> getOrderSearch(int current,String enterpriseCode, String title, String create, String response, String date) {
        List<WorkOrderDetialResponse.WorkOrder> list = new ArrayList<>();
        try {
            String userId = CacheTask.getInstance().getUserId();
            QueryBuilder<WorkOrderDetialResponse.WorkOrder, Integer> builder = workOrderDao.queryBuilder().orderBy("id", false);
            Where<WorkOrderDetialResponse.WorkOrder, Integer> where = builder.where();
            where.eq("enterpriseCode",enterpriseCode);
//            where.eq("localUserId",userId);
            where.and().ne("workState","1");
            if (!TextUtils.isEmpty(title))where.and().like("titleName","%"+title+"%");
            if (!TextUtils.isEmpty(create))where.and().like("createUserName","%"+create+"%");
            if (!TextUtils.isEmpty(response))where.and().like("responUser","%"+response+"%");
            if (!TextUtils.isEmpty(date))where.and().like("createDate","%"+date+"%");
            switch (current){
                case 0:
                    where.and().eq("createUserId",userId);
                    break;
                case 1:
                    where.and().eq("responUserId",userId);
//                    where.and().eq("localUserId",userId);
                    break;
                case 2:
                    where.and().eq("wotype",3);
                    where.and().eq("localUserId",userId);
                    break;
            }
            list = where.query();
            return list;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public List<WorkOrderDetialResponse.WorkOrder> getCreateWO(String userId, String enterpriseCode) {
        RceLog.d("lml","userId"+userId+"enterpriseCode"+enterpriseCode);
        List<WorkOrderDetialResponse.WorkOrder> list = new ArrayList<>();
        QueryBuilder<WorkOrderDetialResponse.WorkOrder, Integer> builder = workOrderDao.queryBuilder().orderBy("id",false);
        try {
            builder.where().eq("createUserId",userId).and().eq("enterpriseCode",enterpriseCode);
            list = builder.query();
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    //根据讨论组ID 查找工单是否查找
    public WorkOrderDetialResponse.WorkOrder getOrderByDisId(String disId) {
        List<WorkOrderDetialResponse.WorkOrder> list = new ArrayList<>();
        QueryBuilder<WorkOrderDetialResponse.WorkOrder, Integer> builder = workOrderDao.queryBuilder();
        try {
            list = builder.where().eq("chatId", disId).query();
            if (list.size()>0){
                return list.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}